import { DatePicker } from "antd"
import moment from 'moment'
import zhCN from 'antd/lib/locale/zh_CN'

const InputDate = ({
  maxDate,
  minDate,
  ...props
}) => {

  const handleChange = (value) => {
    props.onChange(value ? moment(value).format('YYYY-MM-DD') : undefined)
  }

  const disabledDate = (current) => {
    let result = true
    if (maxDate) {
      result = result && (current && current < moment(maxDate).endOf('day'))
    }
    if (minDate) {
      result = result && (current && current > moment(minDate).endOf('day'))
    }
    return !result
  }

  return <DatePicker
    {...props}
    locale={zhCN}
    disabledDate={disabledDate}
    value={props.value ? moment(props.value) : undefined}
    onChange={handleChange}
  ></DatePicker>

}

InputDate.propsMap = [
  {
    type: 'date',
    name: 'maxDate',
    label: '最大日期',
    inputProps: {
      allowClear: true
    }
  },
  {
    type: 'date',
    name: 'minDate',
    label: '最小日期',
    inputProps: {
      allowClear: true
    }
  }
]

export default InputDate